<template>
	<view class="content" v-if="show==1">
		<image class="logo" src="../../static/images/logo.png" mode="widthFix"></image>
		<view class="login" v-if="type==1">
			<view class="login2">
				<image src="../../static/phone.png" mode="aspectFit"/>
				<view class="login3">
					<input name="mobile" @input="shoujihao" type="text" placeholder="请输入手机号"/>
				</view>
			</view>
			<!--<view class="login2" v-if="type==1">
				<image src="../../static/pwd.png" mode="aspectFit"/>
				<view class="login3">
					<input name="password" @input="mima" type="password" password="true" placeholder="请输入登录密码"/>
				</view>
			</view>-->
			<view class="login2">
				<view class="login30">
					<input name="mobile" @input="yan" type="text" placeholder="短信验证码"/>
				</view>
				<view class="zhuce" @click="send">{{yzm}}</view>
			</view>
			<!--<view class="login5">
				<navigator v-if="type==1" url="/pages/login/pwd" class="login7">忘记密码</navigator>
			</view>-->
			<view class="login4" @click="login">登 录</view>
			
		</view>
		<view class="login" v-if="type==3" style="margin-top: 80px;margin-bottom: 20px;">
			<view class="login4" @click="benji">本机一键登录</view>
		</view>
		<view class="login" v-if="type==2" style="margin-top: 80px;margin-bottom: 20px;">
			<button class="login4w" type="default" open-type="getPhoneNumber" @getphonenumber="getmobile">一键登录</button>
		</view>
		<view class="login6">
			<checkbox-group @change="xieyi2">
				<label>
					<checkbox class="xuanzen" :checked="is_xy==1?true:false" value="1" color="#05c160" />登录即表示同意
				</label>
			</checkbox-group>
			<text @click="xieyi">《用户协议》</text><text style="color:#999;">和</text><text @click="yinsi">《隐私政策》</text>
		</view>
		<view class="dibu">
			<view class="dibu2" v-if="type!=1" @click="qie">
				<image src="../../static/images/code.png" mode="aspectFit"></image>
				<view>短信登录</view>
			</view>
			
			<view class="dibu2" v-if="type!=3 && is_yj==true" @click="qie">
				<image src="../../static/images/yijian.png" mode="aspectFit"></image>
				<view>一键登录</view>
			</view>
			<view class="dibu2" v-if="type==1 && is_xcx==1" @click="qie2">
				<image src="../../static/images/yijian.png" mode="aspectFit"></image>
				<view>一键登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app=getApp();
	var dingshi;
	export default {
		data() {
			return {
				mobile:'',
				show:0,
				pid:0,
				pidtype:0,
				password:'',
				code:'',
				is_xcx:0,
				type:1,
				is_xy:0,
				is_yj:false,
				yzm:'获取验证码',
				time:60,
				vio:0,
				is_send:false
			}
		},
		onLoad(op) {
			var that=this;
			var pid=uni.getStorageSync('pid');
			if(app.isBlank(pid)==false){
				this.pid=pid;
				this.pidtype=2;
			}
			
			if(app.isBlank(op.uid)==false){
				this.pid=op.uid;
				this.pidtype=1;
			}
			
			
			
			
			if(op.vio==1){
				this.vio=1;
			}
			// #ifdef  MP-WEIXIN
			that.type=2;
			this.is_xcx=1;
			this.is_xy=1;
			// #endif
			// #ifndef  APP-PLUS
			that.show=1;
			return false;
			// #endif
			uni.getSystemInfo({
			    success: function (res) {
					if(res.platform=='android'){
						that.is_xy=0;
					}else{
						that.is_xy=1;
					}
			    }
			});
			uni.preLogin({
			    provider: 'univerify',
			    success(e){  //预登录成功
			        // 显示一键登录选项
					that.is_yj=true;
					that.type=3;
					that.show=1;
			    },
			    fail(res){  // 预登录失败
			        // 不显示一键登录选项（或置灰）
			    // 根据错误信息判断失败原因，如有需要可将错误提交给统计服务器
					that.show=1;
			    }
			})
			
		},
		methods: {
			login(){
				var that=this;
				if(app.isBlank(this.mobile)==true){
					uni.showToast({
					    title: '请输入手机号',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				
				if(app.isBlank(this.code)==true){
					uni.showToast({
					    title: '请输入短信验证码',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				if(this.is_xy!=1){
					uni.showToast({
					    title: '请阅读并同意用户协议和隐私政策',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '请求中'
				});
				uni.request({
				    url: app.globalData.url+"index/login",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {mobile:that.mobile,password:that.password,code:that.code,type:that.type,pid:that.pid,pidtype:that.pidtype},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
							icon:'success',
						    duration: 1500
						});
						uni.setStorageSync('uid', res.data.data.uid);
						if(that.pid>0){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								// #endif
							}
						}
						if(that.vio==1){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								beforePage.getdata();
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								beforePage.$vm.getdata();
								// #endif
							}
						}
						setTimeout(function(){
							that.tui();
						},1500)
				    }
				});
			},
			mlogin(mobile){
				var that=this;
				if(app.isBlank(mobile)==true){
					uni.showToast({
					    title: '登录失败，请重试',
						icon:'none',
					    duration: 1000
					});
					return false;
				}
				if(this.is_xy!=1){
					uni.showToast({
					    title: '请阅读并同意用户协议和隐私政策',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '登录中'
				});
				uni.request({
				    url: app.globalData.url+"index/login",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {mobile:mobile,type:that.type,pid:that.pid,pidtype:that.pidtype},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
							icon:'success',
						    duration: 1500
						});
						uni.setStorageSync('uid', res.data.data.uid);
						if(that.pid>0){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								// #endif
							}
						}
						if(that.vio==1){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								beforePage.getdata();
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								beforePage.$vm.getdata();
								// #endif
							}
						}
						setTimeout(function(){
							that.tui();
						},1500)
				    }
				});
			},
			getmobile(e){
				var that=this;
				if(app.isBlank(e.detail.encryptedData)==true){
					return false;
				}
				uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
					that.wxlogin(e.detail.encryptedData,e.detail.iv,loginRes.code);
				  },
				  fail(){
					  uni.showToast({
					    title: "登录失败，请重试",
					  	icon:'none',
					    duration: 1500
					  });
				  }
				});
			},
			wxlogin(encryptedData,iv,code){
				var that=this;
				if(app.isBlank(encryptedData)==true){
					uni.showToast({
					    title: '登录失败，请重试',
						icon:'none',
					    duration: 1000
					});
					return false;
				}
				if(app.isBlank(iv)==true){
					uni.showToast({
					    title: '登录失败，请重试',
						icon:'none',
					    duration: 1000
					});
					return false;
				}
				if(app.isBlank(code)==true){
					uni.showToast({
					    title: '登录失败，请重试',
						icon:'none',
					    duration: 1000
					});
					return false;
				}
				if(this.is_xy!=1){
					uni.showToast({
					    title: '请阅读并同意用户协议和隐私政策',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '登录中'
				});
				uni.request({
				    url: app.globalData.url+"index/wxlogin",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {code:code,encryptedData:encryptedData,iv:iv,pid:that.pid,pidtype:that.pidtype},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
							icon:'success',
						    duration: 1500
						});
						uni.setStorageSync('uid', res.data.data.uid);
						if(that.pid>0){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								// #endif
							}
						}
						if(that.vio==1){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								beforePage.getdata();
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								beforePage.$vm.getdata();
								// #endif
							}
						}
						setTimeout(function(){
							that.tui();
						},1500)
				    }
				});
			},
			xieyi2(e){
				var x=e.detail.value;
				if(x[0]==1){
					this.is_xy=1;
				}else{
					this.is_xy=0;
				}
			},
			send(){
				if(this.is_send==true){
					return false;
				}
				var that=this;
				if(app.isBlank(this.mobile)==true){
					uni.showToast({
					    title: '请输入手机号',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '发送中'
				});
				uni.request({
				    url: app.globalData.url+"index/sms",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {mobile:that.mobile,type:3},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
							icon:'none',
						    duration: 1000
						});
						this.is_send=true;
						dingshi=setInterval(function(){
							that.shijian();
						},1000);
				    }
				});
			},
			
			shijian(){
				this.yzm=this.time+"S";
				this.time=this.time-1;
				if(this.time<0){
					this.yzm="重新获取";
					this.time=60;
					this.is_send=false;
					clearInterval(dingshi);
				}
			},
			benji(){
				if(this.is_xy!=1){
					uni.showToast({
					    title: '请先阅读并同意用户协议和隐私政策',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				var that=this;
				uni.login({
				    provider: 'univerify',
				    univerifyStyle: { 
						fullScreen: false,
						"authButton": {  
						    "normalColor": "#05c160"
						},
						"otherLoginButton": {
							"title": "其他手机号登录"
						},
						"privacyTerms":{
							"defaultCheckBoxState":false
						}
					},
					success: function (res) {
						uniCloud.callFunction({
						  name: 'yijiandenglu', // 你的云函数名称
						  data: {
						    'access_token': res.authResult.access_token, // 客户端一键登录接口返回的access_token
						    'openid': res.authResult.openid // 客户端一键登录接口返回的openid
						  }
						}).then(res => {
						  // 登录成功，可以关闭一键登录授权界面了
						  if(app.isBlank(res.result.mobile)==true){
							  uni.showToast({
							    title: "登录失败，请重试",
							  	icon:'none',
							    duration: 1000
							  });
							  uni.closeAuthView();
							  return false;
						  }
						  uni.closeAuthView();
						  that.mlogin(res.result.mobile);
						}).catch(err=>{
						  // 处理错误
						  console.log(err);
						  uni.showToast({
						    title: "登录失败，请重试",
						  	icon:'none',
						    duration: 1000
						  });
						  uni.closeAuthView();
						})
					},
					fail: function (res) {
					    if(res.errCode==30002){
							that.type=1;
							uni.closeAuthView();
						}
					}
				})
			},
			qie(){
				
				if(this.type>1){
					this.type=1;
				}else{
					this.type=3;
					//this.benji();
				}
			},
			qie2(){
				this.type=2;
			},
			tui(){
				var pages = getCurrentPages(); //当前页面栈
				if (pages.length > 1){
					uni.navigateBack({
						delta: 1
					});
				}else{
					uni.reLaunch({
					    url: '/pages/index/index'
					});
				}
			},
			shoujihao: function(event) {
			    this.mobile = event.target.value
			},
			mima: function(event) {
			    this.password = event.target.value
			},
			zhuce(){
				uni.navigateTo({
				    url: '/pages/login/zhuce'
				});
			},
			xieyi(){
				uni.navigateTo({
				    url: '/pages/user/danye?id=1'
				});
			},
			yinsi(){
				uni.navigateTo({
				    url: '/pages/user/danye?id=2'
				});
			},
			yan: function(event) {
			    this.code = event.target.value
			}
		}
	}
</script>

<style>
	.zhuce{width:120px;height:40px;line-height: 40px;text-align: center;background-color: #05c160;color:#fff;margin-left: 10px;}
	.login30 input{width:100%;height:40px;text-align: center;}
	.login30{width:100%;height:40px;flex:1;}
	.dibu2 view{width:100%;color:#666;text-align: center;}
	.dibu2 image{width:25px;height:25px;}
	.dibu2{width:100px;text-align: center;display: inline-block;}
	.dibu{width:80%;margin: 0px auto;overflow: hidden;margin-top: 30px;text-align: center;}
	.xuanzen{transform:scale(0.7);}
	label{line-height: 25px;color: #999;}
	.login7{color:red;float: right;margin-right: 5px;}
	.login6{color:#05c160;float: left;line-height: 25px;margin-top: 15px;font-size: 0.75rem;}
	.login6 text,checkbox-group{float: left;}
	.login5{width:100%;height:30px;line-height: 30px;overflow: hidden;margin-top: -15px;}
	.login4w{width:100%;height:40px;line-height: 40px;text-align: center;background-color: #05c160!important;color:#fff!important;border:0px!important;font-size: 0.85rem;}
	.login4{width:100%;height:40px;line-height: 40px;text-align: center;background-color: #05c160;color:#fff;border-radius: 3px;margin-top: 30px;}
	.login3 input{width:100%;height:40px;}
	.login3{width:100%;height:40px;flex:1;}
	.login2 image{width:22px;height:22px;margin-right: 8px;margin-top: 9px;margin-left: 5px;}
	.login2{width:100%;height:40px;background-color: #ececec;border-radius: 3px;display: flex;margin-bottom: 20px;}
	.login{width:80%;}
	.logo{width:180px;margin: 60px auto;}
</style>
